package com.jumper.terry

import org.gradle.api.Plugin
import org.gradle.api.Project

public class CopyApkPlugin implements Plugin<Project> {


    def static final BUILD_NULL = 0;

    def static final BUILD_TYPE_APPLICATION = 1;

    def static final BUILD_TYPE_LIBRARY = 2;

    def config;

    def buildType;



    public static boolean isEmpty(CharSequence s) {
        return s == null?true:s.length() == 0;
    }


    @Override
    void apply(Project project) {


        project.logger.error("welcome..this is the first plugin test .....");

        project.extensions.create(AppConst.USER_CONFIG, PluginConfigs);
        config = project.extensions.copyApkPlugin;

        project.task("showName") << {

            println(config.version);
        }


        buildType = BUILD_NULL;
        if (project.plugins.hasPlugin('com.android.application')) {
            buildType = BUILD_TYPE_APPLICATION;
        } else if (project.plugins.hasPlugin('com.android.library')) {
            buildType = BUILD_TYPE_LIBRARY;
        }


        project.afterEvaluate {

            project.logger.error("----------------------CopyApkPlugin build warning ------------------------------------")


            if (buildType == BUILD_NULL) {
                project.logger.error("---------------------- the project is not android project or library --------------------")
            } else if (buildType == BUILD_TYPE_APPLICATION) {

                project.logger.error("---------------------- the project is android project --------------------")

                def android = project.extensions.android
                def apkNameVersion = "";
                if (isEmpty(config.version)) {
                    apkNameVersion = android.defaultConfig.versionName;
                } else {
                    apkNameVersion = config.version;
                }

                android.applicationVariants.all { variant ->

                    def taskName = variant.name;
                    project.logger.error("${AppConst.USER_CONFIG} the taskName  --->${taskName}")

                    project.tasks.all {
//                        project.logger.error("${AppConst.USER_CONFIG} task.all ${it.name}");

                        if ("assemble${taskName.capitalize()}".equalsIgnoreCase(it.name)) {
                            it.doLast {
                                project.copy {
                                    from "${project.buildDir}/outputs/apk/${project.getName()}-${taskName}.apk"
                                    into "${config.bakApkPath}"
                                    rename { String fileName ->
                                        fileName.replace("${project.getName()}-${taskName}.apk",
                                                "${project.getName()}-${taskName}-${config.someWordsBeforeVersion}-${apkNameVersion}.apk")
                                    }
                                }
                            }
                        }
                    }
                }

            } else if (buildType == BUILD_TYPE_LIBRARY) {

                project.logger.error("---------------------- the project is android library --------------------")

                def android = project.extensions.android
                def apkNameVersion = "";
                if (isEmpty(config.version)) {
                    apkNameVersion = android.defaultConfig.versionName;
                } else {
                    apkNameVersion = config.version;
                }

                android.libraryVariants.all { variant ->

                    def taskName = variant.name;
                    project.logger.error("${AppConst.USER_CONFIG} the taskName  --->${taskName}")

                    // 从所有的任务中去找
                    project.tasks.all {

//                        project.logger.error("${AppConst.USER_CONFIG} task.all ${it.name}");

                        if ("assemble${taskName.capitalize()}".equalsIgnoreCase(it.name)) {
                            it.doLast {
                                project.copy {
                                    from "${project.buildDir}/outputs/aar/${project.getName()}-${taskName}.aar"
                                    into "${config.bakApkPath}"
                                    rename { String fileName ->
                                        fileName.replace("${project.getName()}-${taskName}.aar",
                                                "${project.getName()}-${taskName}-${config.someWordsBeforeVersion}-${apkNameVersion}.aar")
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }


}